//
// Copyright (c) Microsoft Corporation.  All rights reserved.
//
//
// Use of this sample source code is subject to the terms of the Microsoft
// license agreement under which you licensed this sample source code. If
// you did not accept the terms of the license agreement, you are not
// authorized to use this sample source code. For the terms of the license,
// please see the license agreement between you and Microsoft or, if applicable,
// see the LICENSE.RTF on your install media or the root of your tools installation.
// THE SAMPLE SOURCE CODE IS PROVIDED "AS IS", WITH NO WARRANTIES OR INDEMNITIES.
//
//
// (C) Copyright 2006 Marvell International Ltd.
// All Rights Reserved
//

#include <bsp.h>
#include "ebootcfg.h"
//------------------------------------------------------------------------------
//
//  Function:  OALArgsQuery
//
//  This function is called from other OAL modules to return boot arguments.
//  Boot arguments are typically placed in fixed memory location and they are
//  filled by boot loader. In case that boot arguments can't be located
//  the function should return NULL. The OAL module then must use default
//  values.
//
void ResetDefaultBSPCFG(BSP_ARGS *pArg)
{

    OALLog(L"\r\nResetting factory default configuration...\r\n");

    memset(pArg, 0, sizeof(BSP_ARGS));

    pArg->header.signature = OAL_ARGS_SIGNATURE;
    pArg->header.oalVersion= OAL_ARGS_VERSION  ;
    pArg->header.bspVersion= BSP_ARGS_VERSION;
    pArg->argsLength = sizeof(BSP_ARGS);

    pArg->DefaultBlit = BSP_ARGS_BLIT_OPT;
    pArg->L2CacheEnable = FALSE;

    pArg->SpecialECO = FALSE;

    pArg->IMM_ENABLE = TRUE;
    pArg->RunFreq = BSP_ARGS_RUN_FREQ_5;
    pArg->BatteryEnable = FALSE;
    pArg->kitl.devLoc.PhysicalLoc= (PVOID)(MONAHANS_BASE_REG_PA_USB2H);
    pArg->kitl.flags = 0;
    pArg->DefaultTouchDriver = BSP_ARGS_WM9713_TOUCH;

	pArg->STUARTUsage = BSP_ARGS_STUART_DEBUG_SERIAL;
    pArg->FFUARTUsage = BSP_ARGS_FFUART_2;
    pArg->TechBoardUsage = BSP_ARGS_TECHBOARD_NONE;
    pArg->NAND_DMA_enable= TRUE;
    pArg->NAND_INTR_enable= FALSE;
    pArg->NAND_ClockGatingWA_enable=TRUE;


    pArg->eBootCFG.autoDownloadImage = TRUE;
    pArg->kitl.ipAddress             = 0x0150A8C0; //inet_addr("192.168.80.1");
    pArg->kitl.ipMask			     = 0x00FFFFFF; //255.255.255.0
    pArg->eBootCFG.numBootMe         = 50;
    pArg->eBootCFG.delay             = 3;
    pArg->eBootCFG.dwLaunchAddr      = 0;
    pArg->eBootCFG.dwPhysStart       = 0;
    pArg->eBootCFG.dwPhysLen         = 0;
    pArg->eBootCFG.bootDeviceOrder   = 0;
    pArg->eBootCFG.dwStoreAddr       = 0;
    pArg->eBootCFG.CheckSignatures   = FALSE;
    pArg->eBootCFG.ConfigMagicNumber = EBOOT_CFG_MAGIC_NUMBER;
    // Kitl transport
    pArg->kitl.mac[0]		 = 0x0200;
    pArg->kitl.mac[1]		 = 0xC3B3;
    pArg->kitl.mac[2]		 = 0x115F;
    // When reset, make sure updating the BSPArgs as well
    pArg->bUpdateMode		 = FALSE;
    pArg->LowPowerModeIdle   = 0;  // Low powermode flag will be configured by register item now. (BSP_ARGS_IDLE_D0CS | BSP_ARGS_IDLE_D2;)
    //	pArg->eBootCFG.drvCfg[CFG_LCD_PANEL] = LCD_PANEL_766C;

}


VOID* OALArgsQuery(UINT32 type)
{
    VOID *pData = NULL;
    BSP_ARGS *pArgs = (BSP_ARGS *)IMAGE_SHARE_ARGS_UA_START;

    OALMSG(OAL_ARGS&&OAL_FUNC, (L"+OALArgsQuery(%d)\r\n", type));

    // Check if there is expected signature
    if (
        pArgs->header.signature  != OAL_ARGS_SIGNATURE ||
        pArgs->header.oalVersion != OAL_ARGS_VERSION   ||
        pArgs->header.bspVersion != BSP_ARGS_VERSION   ||
        pArgs->argsLength != sizeof (BSP_ARGS)
    )
    {
        OALMSGS(TRUE, (L"Invalid BSP Args. Reset to default value"));
        ResetDefaultBSPCFG(	pArgs);
    }


    // Depending on required args
    switch (type) {

    case OAL_ARGS_QUERY_DEVID:
        pData = &pArgs->deviceId;
        break;

    case OAL_ARGS_QUERY_KITL:
        pData = &pArgs->kitl;
        break;

    case OAL_ARGS_QUERY_UUID:
        // Implement BSP device specific UUID. If not, the platform base code
        // will generate a UUID from the deviceId and part of a fixed pattern.
        // pData = &pArgs->uuid;
        break;

    case BSP_ARGS_QUERY_DBGSERIAL:
        //if ((pArgs->dbgSerPhysAddr != MONAHANS_BASE_REG_PA_BTUART) && (pArgs->dbgSerPhysAddr != MONAHANS_BASE_REG_PA_FFUART))
        //{
            pArgs->dbgSerPhysAddr = MONAHANS_BASE_REG_PA_FFUART;
        //}
        pData = &pArgs->dbgSerPhysAddr;
        break;

    case OAL_ARGS_QUERY_UPDATEMODE:
        {
            pData = &pArgs->bUpdateMode;
            OALMSG(1, (L"OAL_ARGS_QUERY_UPDATEMODE return %d\r\n", pArgs->bUpdateMode));
        }
        break;

    case BSP_ARGS_QUERY_FORMATPARTITION:
        pData = &pArgs->bFormatPartition;
        OALMSG(1, (TEXT("BSP_ARGS_QUERY_FORMATPARTITION return %d\r\n"), pArgs->bFormatPartition));
        break;

    case BSP_ARGS_QUERY_L2CACHE:
        pData =&pArgs->L2CacheEnable;

    }


    OALMSG(OAL_ARGS&&OAL_FUNC, (L"-OALArgsQuery(pData = 0x%08x)\r\n", pData));
    return pData;
}


//This function is call in startup.s.
//Don't call any kernel function because kernel have not started.
BOOL IsL2Enable()
{
    BSP_ARGS *pArgs = (BSP_ARGS *)IMAGE_SHARE_ARGS_UA_START;
    if (
        pArgs->header.signature  != OAL_ARGS_SIGNATURE ||
        pArgs->header.oalVersion != OAL_ARGS_VERSION   ||
        pArgs->header.bspVersion != BSP_ARGS_VERSION   ||
        pArgs->argsLength != sizeof (BSP_ARGS)
    )
    {
        return FALSE;
    }

    return pArgs->L2CacheEnable;
}
//------------------------------------------------------------------------------

